package com.ibm.cps.demo;

import java.util.HashMap;
import java.util.Map;

import com.ibm.cps.demo.PseudoDataProducer.NumberGenerator;
import com.ibm.cps.messagequeue.AMQPClient;
import com.ibm.datamodel.timeseries.SchemaField;
import com.ibm.factories.TimeSeriesSchemaFactory;
import com.ibm.interfaces.IMessageClient;
import com.ibm.interfaces.ITimeSeriesSchema;
import com.ibm.util.DataTypes;
import com.ibm.util.exception.CPSException;

public class SampleDataProducer {
	private static final SchemaField[] fields = new SchemaField[]{
		new SchemaField("random", DataTypes.DT_DOUBLE, SchemaField.FieldType.NORMAL),
		new SchemaField("sinusoidal", DataTypes.DT_DOUBLE, SchemaField.FieldType.NORMAL)
	};
	
	public static void main(String[] args) throws CPSException, InterruptedException{
		if(args.length != 6){
			System.err.println("Invalid argument list, 6 arguments are required, [host, port, username, password, topicname, tenantId]");
			return;
		}
		String host = args[0];
		int port = Integer.parseInt(args[1]);
		String username = args[2];
		String password = args[3];
		String topicname = args[4];
		String tenantId = args[5];
		
		IMessageClient client = new AMQPClient(host, port, username, password);
		ITimeSeriesSchema schema = TimeSeriesSchemaFactory.createFromFieldDefinition(tenantId, "FAKE_SCHEMA_ID", fields);
		Map<String, NumberGenerator> generators = new HashMap<>();
		generators.put("random", new PseudoDataProducer.RandomGenerator(5, 10));
		generators.put("sinusoidal", new PseudoDataProducer.SinusoidalGenerator(10));
		PseudoDataProducer producer = new PseudoDataProducer(schema, generators);
		int indicator = 0;
		long timeseriesKey = 1L;
		while(true){
			indicator ++;
			byte[] message = producer.produce(timeseriesKey, indicator);
			client.publishMessage(topicname, message);	
			Thread.sleep(1000L);
		}
	}

}
